var PersonCapacityBoard = function () {
  var template = /* html */ `
    <div class="person-capacity-board" v-loading.fullscreen.lock="loading">
      <template v-if="isShow">
      <search-btns v-if="search!==null" ref="searchBtns" :has-search="true" search-btn-text="查询" search-title="查询条件" @after-leave="afterLeave"
        mode="dialog">
        <div slot="search" slot-scope="scope">
          <search-form :search="search" :dropdownData="dropdownList" :dept-work-name="deptWorkName" @search-data="searchData"></search-form>
        </div>
      </search-btns>
      <div class="capacity-board-table-div" :key="boardKey">
        <template v-if="capacityData!==null">
          <person-capacity-table ref="capacityTable" :capacity-data="capacityData"></person-capacity-table>
        </template>
        <div v-else class="no-data">
          <div v-if="userType===3">没有权限查看员工产能数据</div>
          <div v-else-if="search===null">未设置部门或车间</div>
          <div v-else>暂无员工产能数据</div>
        </div>
      </div>
      </template>
    </div>
  `;

  return {
    mixins: [boardRunningMixin, boardMixin(false)],

    data: function () {
      return {
        loading: false,
        isShow: false,
        capacityData: null,
        search: null,
        userType: null,
        boardKey: SysUtil.getId(),
        dropdownList: []
      };
    },

    created: function () {
      var self = this;
      this.loading = true;
      this.setUser().then(function () {
        if (self.search === null) {
          self.capacityData = null;
          self.isShow = true;
          self.loading = false;
          return;
        }

        return self.setCapacityData(true, true).then(function (capacityData) {
          self.capacityData = capacityData;
          self.isShow = true;
          self.loading = false;
          if (capacityData === null) {
            return;
          }

          var search = self.search;
          capacityData.resetCapacityList(search.deptWork, search.searchType);
          var dropdownList = capacityData.getDropdownData(search.searchType);
          self.dropdownList = dropdownList;
          capacityData.toSearchData(search);
          self.boardRunning();
        });
      }).catch(function () {
        self.isShow = true;
        self.loading = false;
      });
    },

    methods: {
      setUser: function () {
        var self = this;
        return SysUtil.getUser().then(function (user) {
          var search = new PersonCapacityInfoSearch(user);
          self.userType = search.userType;
          if (search.userType === 3) {
            self.search = null;
            return user;
          }

          self.search = search;
          var deptWork = search.deptWork;
          if (deptWork.length === 0) {
            return user;
          }
          return self.setDeptWorkName(deptWork, user);
        });
      },

      setCapacityData: function (hasAlert, isNew) {
        var search = this.search;
        if (search === null) {
          return Promise.resolve(null);
        }
        var month = search.month;
        month = moment(month).format('YYYY-MM');

        var self = this;
        return ajaxRequest('displayView/getUserProcessDisplayViewData', {
          month: month
        }, true, hasAlert).then(function (capacityList) {
          var capacityData = self.capacityData;
          if (isNew) {
            capacityData = new PersonCapacityInfo(capacityList, month);
          } else {
            capacityData.resetData(capacityList);
          }
          return capacityData;
        });
      },

      afterLeave: function () {
        this.search.resetOldData();
      },

      searchData: function () {
        var search = this.search;
        var month = search.month;
        var oldMonth = search.getOldData('month');
        this.clearTimeoutList();
        this.clearTableTimeoutList();
        this.loading = true;
        var self = this;
        if (!moment(month).isSame(oldMonth, 'month')) {
          return this.setCapacityData(true, true).then(function (capacityData) {
            self.capacityData = capacityData;
            self.search.saveOldData();
            self.$refs['searchBtns'].closePopover();
            if (capacityData === null) {
              return;
            }

            var search = self.search;
            capacityData.resetCapacityList(search.deptWork, search.searchType);
            var dropdownList = capacityData.getDropdownData(search.searchType);
            self.dropdownList = dropdownList;
            capacityData.toSearchData(search);
            self.boardKey = SysUtil.getId();
            self.boardRunning();
            self.loading = false;
          }).catch(function () {
            self.loading = false;
          });
        }

        setTimeout(function () {
          self.search.saveOldData();
          self.$refs['searchBtns'].closePopover();
          var capacityData = self.capacityData;
          capacityData.toSearchData(self.search);
          capacityData.rowIndex = 0;
          capacityData.colIndex = 0;
          self.boardKey = SysUtil.getId();
          self.boardRunning();
          self.loading = false;
        }, 300);
      },

      boardRunning: function () {
        this.clearTimeoutList();

        this.capacityListRunning();
      },

      capacityListRunning: function () {
        var refreshTime = 1 * 3600 * 1000;
        var self = this;
        var timeout = setTimeout(function () {
          if (self.canRefresh()) {
            self.clearTableTimeoutList();
            self.setCapacityData().then(function(capacityData){
              var search = self.search;
              capacityData.resetCapacityList(search.deptWork, search.searchType);
              var dropdownList = capacityData.getDropdownData(search.searchType);
              self.dropdownList = dropdownList;
              capacityData.toSearchData(search);
              self.capacityListRunning();
            });
            self.timeoutList[0] = null;
          } else {
            self.timeoutList[0] = null;
            self.capacityListRunning();
          }
        }, refreshTime);
        this.timeoutList[0] = timeout;
      },

      canRefresh: function () {
        var capacityData = this.capacityData;
        var month = capacityData.month;
        if (moment().isSame(month, 'month')) {
          return true;
        }

        if (moment().isAfter(month, 'month')) {
          var hour = moment().get('hour');
          if (hour <= 1) {
            return true;
          }
        }

        return false;
      },

      clearTableTimeoutList: function () {
        if (this.$refs['capacityTable']) {
          this.$refs['capacityTable'].clearTimeoutList();
        }
      }
    },

    components: {
      SearchBtns: SearchBtns,
      SearchForm: PersonCapacitySearchForm,
      PersonCapacityTable: PersonCapacityTable
    },

    template: template
  };
}();
